<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择任务状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.chat_mass_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:chatWsMassTaks:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:chatWsMassTaks:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="chatWsMassTaksList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false" />
      <el-table-column label="任务名称" align="center" prop="name" />
      <el-table-column label="在线/账号总数" align="center" prop="onlineNum" />
      <el-table-column label="已完成/群发总数" align="center" prop="allNum" />
      <el-table-column
        label="话术完整推送数"
        align="center"
        prop="successNum"
      />
      <el-table-column label="剩余数据" align="center" prop="residueNum" />
      <el-table-column label="任务状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.chat_mass_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:chatWsMassTaks:edit']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:chatWsMassTaks:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:chatWsMassTaks:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改WS群发任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="WS账号群组" prop="wid">
          <el-select
            v-model="form.wid"
            placeholder="请选择WS账号群组"
            style="width: 100%"
          >
            <el-option
              v-for="dict in groupList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="WS数据" prop="sid">
          <el-select
            v-model="form.sid"
            placeholder="请选择WS数据"
            style="width: 100%"
          >
            <el-option
              v-for="dict in wsData"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务开始时间" prop="startTime">
          <el-date-picker
            style="width: 100%"
            clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择任务开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务结束时间" prop="endTime">
          <el-date-picker
            style="width: 100%"
            clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择任务结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <div>
          <label style="margin-left: 68px"
            ><span style="color: red; margin-right: 4px">*</span>群发设置</label
          >
          <div style="margin-top: 20px; margin-left: 15%">
            账号每群发
            <el-input-number v-model="form.massNum" :min="1" />
            条数据则完成账号群发任务。
          </div>
          <div style="margin-top: 20px; margin-left: 15%">
            账号每群发
            <el-input-number v-model="form.massRestNum" :min="1" /> 条数据则休息
            <el-input-number v-model="form.massRestMin" :min="1" /> 分钟。
          </div>
          <div style="margin-top: 20px; margin-left: 15%; margin-bottom: 20px">
            每个账号群发间隔为：<el-input-number
              v-model="form.massIntervalMin"
              :min="1"
            />
            <span style="margin-left: 10px; margin-right: 10px">~</span>
            <el-input-number v-model="form.massIntervalMax" :min="1" /> 秒。
          </div>
        </div>
        <el-form-item label="发送名片" prop="businessCard">
          <el-radio-group v-model="form.businessCard">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="话术语言" prop="language">
          <el-select
            style="width: 100%"
            v-model="form.language"
            placeholder="请选择语言类型"
          >
            <el-option
              v-for="dict in dict.type.chat_language_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="话术内容" prop="verbalTrick">
          <el-input v-model="form.verbalTrick" type="textarea" placeholder="请输入内容" ></el-input> 
          <!-- <editor v-model="form.verbalTrick" :min-height="350" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listChatWsMassTaks,
  getChatWsMassTaks,
  delChatWsMassTaks,
  addChatWsMassTaks,
  updateChatWsMassTaks,
} from "@/api/business/chatWsMassTaks";
import { listChatUserGroup } from "@/api/business/chatUserGroup";
import { listChatWs } from "@/api/business/chatWs";
import { listChatWsMassConfig } from "@/api/business/chatWsMassConfig";
export default {
  name: "ChatWsMassTaks",
  dicts: ["chat_language_type", "chat_mass_status", "sys_yes_no"],
  data() {
    return {
      groupList: [],
      wsData: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // WS群发任务表格数据
      chatWsMassTaksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
        wid: [{ required: true, message: "账号id不能为空", trigger: "blur" }],
        sid: [{ required: true, message: "wsid不能为空", trigger: "blur" }],
        startTime: [
          { required: true, message: "任务开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "任务结束时间不能为空", trigger: "blur" },
        ],
        verbalTrick: [
          { required: true, message: "话术内容不能为空", trigger: "blur" },
        ],
        businessCard: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
        language: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();

    //获取数据账号列表
    listChatUserGroup().then((response) => {
      var gruops = response.rows;
      var groupList = [];
      for (var i in gruops) {
        var info = {};
        info.value = gruops[i].id;
        info.label = gruops[i].name;
        groupList.push(info);
      }
      this.groupList = groupList;
    });

    //获取WS数据列表
    listChatWs().then((response) => {
      var gruops = response.rows;
      var groupList = [];
      for (var i in gruops) {
        var info = {};
        info.value = gruops[i].id;
        info.label = gruops[i].name;
        groupList.push(info);
      }
      this.wsData = groupList;
    });
  },
  methods: {
    /** 查询WS群发任务列表 */
    getList() {
      this.loading = true;
      listChatWsMassTaks(this.queryParams).then((response) => {
        this.chatWsMassTaksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        wid: undefined,
        sid: undefined,
        startTime: undefined,
        endTime: undefined,
        businessCard: undefined,
        status: undefined,
        language: undefined,
        verbalTrick: undefined,
        remark: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加WS群发任务";
      //获取群发配置
      listChatWsMassConfig().then((response) => {
        var massData = response.rows[0];
        this.form.massNum = massData.massNum;
        this.form.massRestNum = massData.massRestNum;
        this.form.massRestMin = massData.massRestMin;
        this.form.massIntervalMin = massData.massIntervalMin;
        this.form.massIntervalMax = massData.massIntervalMax;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getChatWsMassTaks(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.form.wid = Number(this.form.wid)
        this.open = true;
        this.title = "修改WS群发任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateChatWsMassTaks(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addChatWsMassTaks(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除WS群发任务编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delChatWsMassTaks(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/chatWsMassTaks/export",
        {
          ...this.queryParams,
        },
        `chatWsMassTaks_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
